#!/usr/bin/pythonBasics
# -*- coding: UTF-8 -*-

#Author:super

import mysql.connector

try:
	mydb = mysql.connector.connect(
		host = 'localhost',
		port = 3309,
		user = 'root',
		password = 'root',
		database = 'runoob_db'
	)
	mydb.start_transaction()
	mycursor = mydb.cursor()
	data = [
		['tom1','https://www.baidu.com'],
		['tom2','https://www.taobao.com']
	]
	#executemany函数反复执行一条sql语句
	mycursor.executemany('INSERT INTO sites(`name`,url) VALUES(%s,%s)',data)
	if mycursor.rowcount>0:
		print('新增成功')
		mydb.commit()
except Exception as e:
	mydb.rollback()
	if 'mydb' in dir():
		mydb.close()
	raise e
finally:
	if 'mydb' in dir():
		mydb.close()